package rs.arm.letthemusicplay;

import java.util.ArrayList;
import java.util.TreeSet;

import rs.arm.letthemusicplay.utilities.Constants;
import rs.arm.letthemusicplay.utilities.FileUtilities;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class PlaylistPicker extends Dialog {

	public static TreeSet<String> pickedLists = new TreeSet<String>();

	public PlaylistPicker(Context context) {
		super(context);
		setContentView(R.layout.playlist_picker);
		pickedLists.clear();
		ArrayList<String> playlists = FileUtilities.getPlaylists();
		PickerAdapter pickerAdapter = new PickerAdapter(context, playlists);
		final ListView listView = (ListView) findViewById(R.id.play_list);
		listView.setAdapter(pickerAdapter);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				PickerAdapter adapter = (PickerAdapter) listView.getAdapter();
				String plist = (String) adapter.getItem(arg2);
				if (pickedLists.contains(plist)) {
					pickedLists.remove(plist);
				} else {
					pickedLists.add(plist);
				}
			}
		});
	}

	public void okButtonClick(View view) {
		if (pickedLists.isEmpty()) {
			Toast.makeText(getOwnerActivity(),
					getContext().getResources().getText(R.string.choose_playlist),
					Toast.LENGTH_LONG).show();
		}
		else {
			Intent intent = new Intent(getOwnerActivity(), MainActivity.class);
			intent.putExtra(Constants.INTENT_START_FROM, Constants.DIALOG_NAME_PLAYLIST_PICKER);
			getOwnerActivity().startActivity(intent);
			dismiss();
		}
	}

	public void cancelButtonClick(View view) {
		pickedLists.clear();
		dismiss();
	}

	private class PickerAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private ArrayList<String> list;

		public PickerAdapter(Context context, ArrayList<String> list) {
			mInflater = LayoutInflater.from(context);
			this.list = list;
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			final ListHolder holder;
			View v = arg1;
			if (v == null || v.getTag() == null) {
				v = mInflater.inflate(R.layout.custom_list_item, null);
				holder = new ListHolder();
				holder.textView = (TextView) v.findViewById(R.id.playlist_name);
				holder.box = (CheckBox) v.findViewById(R.id.pick_item_box);
				v.setTag(holder);
			} else {
				holder = (ListHolder) v.getTag();
			}
			holder.name = (String) getItem(arg0);
			holder.textView.setText(holder.name);
			holder.box.setChecked(false);
			v.setTag(holder);
			return v;
		}

		public class ListHolder {
			String name;
			TextView textView;
			CheckBox box;
		}
	}

}
